import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
import { ConfigData } from './ptpmc/model/comm/config-data';
import { GlobalDataManager } from './ptpmc/comm/global-data-manager';
import { ReportUrls } from './ptpmc/comm/report.urls.conf';
import { CommService } from './ptpmc/comm/comm.service';
import { SubArea } from './ptpmc/model/report/sub-area';
import { Notifycation } from './ptpmc/comm/notifycation';
import { LocalStorage } from './ptpmc/localstorage/local.storage';
import { UserModel } from './ptpmc/model/sys/user-model';


@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

    constructor(private router: Router, private ls: LocalStorage,
        private commService: CommService) {

    }

    ngOnInit() {
        const config: ConfigData = new ConfigData();
        config.projects = new Array();
        this.commService.getConfig('/assets/config/system-config.json').map(u => u.json()).subscribe(u => {
            config.projects = u;
            GlobalDataManager.SetConfigData(config);
            ReportUrls.BaseUrl = config.projects[0].apiUrl;

            this.commService.post(ReportUrls.BaseUrl + ReportUrls.TVResourceUrl, null)
                .subscribe(model => {
                    if (model.resultCode === 200) {
                        GlobalDataManager.setTVResources(model.data);
                        this.commService.post(ReportUrls.BaseUrl + ReportUrls.SubAreaUrl, new SubArea())
                            .subscribe(el => {
                                if (el.resultCode === 200) {
                                    GlobalDataManager.setSubAreas(el.data);
                                    UserModel.userId = this.ls.get('ptpmc_userId');
                                    UserModel.userName = this.ls.get('ptpmc_userName');
                                    // UserModel.roles = JSON.parse(this.ls.get('ptpmc_roles'));
                                } else {
                                    Notifycation.showNotification('初始化区域失败', 'danger');
                                }
                            });
                    } else {
                        Notifycation.showNotification('初始化工位失败', 'danger');
                    }
                });
        });

        /**
         * navigate to the login page when user land on root
         */
        if (this.router.url === '/') {
            // this.router.navigate(['/dashboard']);
        }
    }

}
